package cn.zjw.demo.grpc;


import cn.zjw.demo.config.SelfASRConfig;
import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;

public class SpeechRecognizerClientFactory extends BasePooledObjectFactory<SpeechRecognizerClient> {
    private SelfASRConfig selfASRConfig;

    @Override
    public SpeechRecognizerClient create() throws Exception {
        return new SpeechRecognizerClient(selfASRConfig.getHost(), selfASRConfig.getPort());
    }

    @Override
    public PooledObject<SpeechRecognizerClient> wrap(SpeechRecognizerClient speechRecognizerClient) {
        return new DefaultPooledObject<>(speechRecognizerClient);
    }

    @Override
    public void destroyObject(PooledObject<SpeechRecognizerClient> p) throws Exception {
        SpeechRecognizerClient client = p.getObject();
        client.shutdown();
        super.destroyObject(p);
    }
}
